package com.huhao.wshare;

import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 为根布局添加顶部边距
        View rootView = findViewById(android.R.id.content);
        rootView.post(() -> {
            int statusBarHeight = getStatusBarHeight(rootView);
            rootView.setPadding(0, statusBarHeight, 0, 0);
        });
    }

    /**
     * 获取状态栏高度
     * @param view 用于获取 WindowInsets 的 View
     * @return 状态栏高度
     */
    private int getStatusBarHeight(View view) {
        WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(view);
        if (insets != null) {
            // 使用兼容方法获取状态栏高度
            return insets.getInsets(WindowInsetsCompat.Type.statusBars()).top;
        }
        return 0;
    }
}